package uit.ce.hungtrung;

import java.util.Vector;

import uit.ce.hungtrung.game.lib.game2d.Game2D;
import uit.ce.hungtrung.game.lib.game2d.MySurfaceView;
import uit.ce.hungtrung.game.lib.game2d.Sprite;
import uit.ce.hungtrung.sound.BackgroundMusicGame;
import uit.ce.hungtrung.sound.SoundManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

public class GameActivity extends Activity implements Game2D, OnClickListener {

	private int SCR_W, SCR_H;

	private int yMin, yMax;

	private boolean isTouchScreen = false;

	private Sprite spHome, spBackGround;
	private Egg myEgg;
	private ManagerBasket managerBasket;

	private boolean isInit = false;

	// sound hieu ung toi da chi co 10 hieu ung, tang them c� the duoc
	private SoundManager objSoundManager;
	// public BackgroundMusic objBackgroundMusic;
	private BackgroundMusicGame objBackgroundMusicGame;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(new MySurfaceView(this, null));
		// load Am thanh
		objSoundManager = new SoundManager(this);

		// objBackgroundMusic = new BackgroundMusic(Constant.musicBackground,
		// this);
		objBackgroundMusicGame = new BackgroundMusicGame();
		isInit = false;

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		objBackgroundMusicGame.execute(this);

		objSoundManager.setSoundRAW2SoundPool(Constant.musicAffect_1,
				Constant.intIndexAffect1);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		objBackgroundMusicGame.stop();
		objSoundManager.unloadAll();
	}

	void demoPlaySoundAffect() {
		objSoundManager.play(objSoundManager
				.getSoundID(Constant.intIndexAffect1));
	}

	public void Init(Canvas canvas) {
		SCR_W = canvas.getWidth();
		SCR_H = canvas.getHeight();
		yMin = 0;
		yMax = SCR_H;

		spBackGround = new Sprite(this, R.drawable.bg);
		spBackGround.setPosition(0, 0);

		myEgg = new Egg(this, SCR_W, SCR_H);

		managerBasket = new ManagerBasket(this, SCR_W, SCR_H);

		myEgg.setPositionInBasket(managerBasket.getBasketIndex(0));

		isInit = true;
	}

	public void Draw(Canvas canvas) {
		if (!isInit) {
			Init(canvas);
		}
		DrawBackGround(canvas);
		if (myEgg.isFly()) {
			DrawBaskets(canvas);
			DrawEgg(canvas);
		} else {
			DrawEgg(canvas);
			DrawBaskets(canvas);
		}

		// Paint myPaint = new Paint();
		// myPaint.setColor(Color.rgb(0, 0, 0));
		// myPaint.setStrokeWidth(10);
		// canvas.drawRect(0, 100, 200, 400, myPaint);
	}

	public void Update() {
		// System.out.println("myEgg.getY() = " + myEgg.getY());
		// System.out.println("basket.getY() = " + basket.getY());
		if (isTouchScreen) {
			isTouchScreen = false;
			myEgg.setFly(true);
		}

		for (int i = 0; i < managerBasket.getLeghtArrBaskets(); i++) {
			if (myEgg.isCollisWith(managerBasket.getBasketIndex(i))) {
				if (!myEgg.isCollis()) {
					myEgg.setCollis(true);
					myEgg.setFly(false);
					myEgg.setPositionInBasket(managerBasket.getBasketIndex(i));
					break;
				} else if (myEgg.isCollis()) {
					myEgg.setPositionInBasketX(managerBasket.getBasketIndex(i));
				}
			}
		}
		myEgg.Update();
		managerBasket.Update();

	}

	public void DrawBackGround(Canvas canvas) {
		spBackGround.DrawIamge(canvas);
	}

	public void DrawEgg(Canvas canvas) {
		myEgg.Render(canvas);
	}

	public void DrawBaskets(Canvas canvas) {
		managerBasket.Render(canvas);
	}

	public void Touch(MotionEvent event) {
		float pX = event.getX();
		float pY = event.getY();
		isTouchScreen = true;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		default:
			break;
		}

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		GotoMainMenu();
	}

	private void GotoMainMenu() {
		Intent intenMainMenu = new Intent(this, MenuActivity.class);
		startActivity(intenMainMenu);
		finish();
	}
}
